package com.kite.ossServer;

import android.webkit.JavascriptInterface;
import com.kite.enums.TropType;
import org.json.JSONObject;

public class Trop {
    public String tropName;
    public Object tropValue;
    public TropType tropType;
    public boolean isExt;

    public Trop() {
        this.tropName="";
        this.tropValue=null;
        this.isExt=false;
    }

    public Trop(String tropName, Object tropValue) {
        this.tropName = tropName;
        this.tropValue = tropValue;
        this.isExt=true;
    }

    // 统一添加值
    public void setValue(Object v) {
        this.tropType = TropType.from(v);
        switch (tropType) {
            case INTEGER: tropValue = (v instanceof Number) ? ((Number) v).intValue()    : 0; break;
            case LONG:    tropValue = (v instanceof Number) ? ((Number) v).longValue()   : 0L; break;
            case DOUBLE:  tropValue = (v instanceof Number) ? ((Number) v).doubleValue() : 0.0; break;
            case BOOLEAN: tropValue = (v instanceof Boolean) ? (Boolean) v : Boolean.parseBoolean(String.valueOf(v)); break;
            case NULL:    tropValue = null; break;
            case STRING:
            default:      tropValue = String.valueOf(v); break;
        }
    }

    //判断当前节点是否有效
    @JavascriptInterface
    public boolean Exted(){
        return isExt;
    }

    //获得Trop名字
    @JavascriptInterface
    public String getTropName(){
        return this.tropName;
    }

    // 一些安全的值获取API
    @JavascriptInterface
    public int     getAsInt()    { return tropType == TropType.INTEGER ? (Integer) tropValue : 0; }
    @JavascriptInterface
    public long    getAsLong()   { return tropType == TropType.LONG    ? (Long)    tropValue : 0L; }
    @JavascriptInterface
    public double  getAsDouble() { return tropType == TropType.DOUBLE  ? (Double)  tropValue : 0.0; }
    @JavascriptInterface
    public boolean getAsBool()   { return tropType == TropType.BOOLEAN ? (Boolean) tropValue : false; }
    @JavascriptInterface
    public String  getAsString() { return String.valueOf(tropValue); }
    @JavascriptInterface
    public String  getRaw()      { return String.valueOf(tropValue); }
    @JavascriptInterface
    public String  toJSONValue() { return String.valueOf(tropValue); }

    // 获取Trop的值(String)
    @JavascriptInterface
    public String  getTropValue() {
        return getAsString();
    }

    // 更新Trop的值
    @JavascriptInterface
    public String changeValue(String v) {
        try {
            setValue(v);

            JSONObject result = new JSONObject();
            result.put("success", true);
            result.put("message", "Trop值更新成功");
            result.put("newValue", String.valueOf(tropValue));
            result.put("tropType", tropType.toString());

            return result.toString();
        } catch (Exception e) {
            return "{\"success\":false,\"error\":\"更新Trop值失败: " + escapeJson(e.getMessage()) + "\"}";
        }
    }

    // 获取Trop类型信息
    @JavascriptInterface
    public String getTropInfo() {
        try {
            JSONObject result = new JSONObject();
            result.put("success", true);
            result.put("tropName", tropName);
            result.put("tropType", tropType.toString());
            result.put("isValid", isExt);
            result.put("valueType", tropValue != null ? tropValue.getClass().getSimpleName() : "null");

            return result.toString();
        } catch (Exception e) {
            return "{\"success\":false,\"error\":\"获取Trop信息失败: " + escapeJson(e.getMessage()) + "\"}";
        }
    }

    // ============ 辅助方法 ============

    private String escapeJson(String value) {
        if (value == null) {
            return "";
        }
        return value.replace("\\", "\\\\")
                   .replace("\"", "\\\"")
                   .replace("\b", "\\b")
                   .replace("\f", "\\f")
                   .replace("\n", "\\n")
                   .replace("\r", "\\r")
                   .replace("\t", "\\t");
    }
}
